#include "CtrlFileList.h"
#include "QsLog.h"
#include "Qslog.h"
#include <GenOpt.h>
#include <QDir>
#include <QFileDialog>
#include <QStandardItemModel>

CtrlFileList::CtrlFileList(QWidget *parent) : QWidget(parent) {
  ui.setupUi(this);
  ui.listWidgetFile->setSelectionMode(QAbstractItemView::SingleSelection);
}

CtrlFileList::~CtrlFileList() {}

void CtrlFileList::on_pbBrowse_clicked() {
  QString file_path = QFileDialog::getExistingDirectory(
      this, "选择视频文件路径", "C:/", QFileDialog::DontResolveSymlinks);
  cur_folder_ = file_path;
  ui.lineEditPath->setText(file_path);
  file_list_.clear();
  CGenOpt::SearchAllFileInPath(file_path, file_list_);
  if (file_list_.count() == 0) {
    QLOG_INFO() << "未找到任何测试的视频文件";
    return;
  }
  QLOG_INFO() << "找到" << file_list_.count() << "个视频文件";
  UpdateFileListOnCtrl();
}

void CtrlFileList::on_pbRefresh_clicked() {
  QString file_path = ui.lineEditPath->text();
  file_list_.clear();
  CGenOpt::SearchAllFileInPath(file_path, file_list_);
  cur_folder_ = file_path;
  if (file_list_.count() == 0) {
    QLOG_INFO() << "未找到任何测试的视频文件";
    return;
  }
  QLOG_INFO() << "找到" << file_list_.count() << "个视频文件";
  UpdateFileListOnCtrl();
}

void CtrlFileList::on_pbClear_clicked() {
  file_list_.clear();
  ui.listWidgetFile->clear();
}

void CtrlFileList::UpdateFileListOnCtrl() {
  if (file_list_.count() == 0) {
    return;
  }
  ui.listWidgetFile->clear();
  ui.listWidgetFile->addItems(file_list_);
}

void CtrlFileList::on_listWidgetFile_clicked() {
  cur_file_ = ui.listWidgetFile->currentItem()->text();
  emit sigFileChange(cur_file_);
}
void CtrlFileList::on_listWidgetFile_doubleClicked() {
  cur_file_ = ui.listWidgetFile->currentItem()->text();
  emit sigFileTest(cur_file_);
}

QString CtrlFileList::GetFilePath() { return cur_file_; }